package cn.otrue.patienthealthmanager.activity;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.bean.TransferParameter;
import cn.otrue.patienthealthmanager.http.HttpCons;
import cn.otrue.patienthealthmanager.http.HttpServer;
import cn.otrue.patienthealthmanager.http.ServerHandler;
import cn.otrue.patienthealthmanager.utils.MyConst;

/**
 * 注册页面
 * 
 * @author G
 * 
 */
public class RegisterActivity extends BaseActivity {
	private EditText et_zhuceyonghun;
	private EditText et_yanzhengma;
	private EditText et_zhucemima;

	private EditText et_zaicimima;
	private Button btn_next;
	private Button bt_yanzhengma;

	// ////////////////////////////////////////////////////注册成功跳转到个人信息页
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView();

	}

	private Handler ReHandler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals("001")) {

				Intent mianIntent = new Intent(RegisterActivity.this,
						LoginActivity.class);
				startActivity(mianIntent);
				finish();
				Toast.makeText(getApplicationContext(), msg, 0).show();
			} else {
				btn_next.setOnClickListener(RegisterActivity.this);
				btn_next.setClickable(true);

				Toast.makeText(getApplicationContext(), msg, 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			btn_next.setOnClickListener(RegisterActivity.this);
			btn_next.setClickable(true);
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(RegisterActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private Handler handlerTime = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				bt_yanzhengma.setText("(" + msg.arg1 + "s)重新获取");

				bt_yanzhengma.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View arg0) {

					}
				});
				bt_yanzhengma.setClickable(false);
				bt_yanzhengma.setBackgroundResource(R.drawable.shape_button);
				break;
			case 1:
				bt_yanzhengma.setText("获取验证码");
				bt_yanzhengma.setOnClickListener(RegisterActivity.this);
				bt_yanzhengma.setClickable(true);
				bt_yanzhengma
						.setBackgroundResource(R.drawable.shape_button_auxiliary_normal);

				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	private Handler Handler = new ServerHandler() {
		@Override
		public void onSuccess(Bundle data, String state, String msg) {
			if (state.equals("001")) {

				new Thread(new Runnable() {

					@Override
					public void run() {

						for (int i = 60; i > 0; i--) {
							Message msg = new Message();
							msg.what = 0;
							msg.arg1 = i;
							handlerTime.sendMessage(msg);
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}

						}
						Message msg = new Message();
						msg.what = 1;
						handlerTime.sendMessage(msg);
					}
				}).start();
				Toast.makeText(getApplicationContext(), msg, 0).show();
			} else {
				bt_yanzhengma.setClickable(true);
				Toast.makeText(getApplicationContext(), msg, 0).show();

			}
		}

		@Override
		public void onFail(Bundle data, String state) {
			if (state.equals(HttpCons.STATE_EXCEPTION)) {
				// 代表进入状态STATE_EXCEPTION 网络异常
				Toast.makeText(RegisterActivity.this, "网络异常,请稍后重试",
						Toast.LENGTH_SHORT).show();
			} else if (state.equals(HttpCons.STATE_FORMAT_ERROR)) {
				// 代表进入STATE_FORMAT_ERROR(不是Json)
			} else {
				// 代表进入STATE_ANALYTICAL_ERROR(Json解析错误)

			}
		}
	};

	private ImageView xiaoyu;

	private void initView() {
		initTitleBar();

		bt_yanzhengma = (Button) findViewById(R.id.bt_yanzhengma);
		bt_yanzhengma.setOnClickListener(this);

		TextView tiaokuan = (TextView) findViewById(R.id.tiaokuan);
		tiaokuan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent mianIntent = new Intent(RegisterActivity.this,
						TiaoKuanActivity.class);
				startActivity(mianIntent);
			}
		});
		xiaoyu = (ImageView) findViewById(R.id.xiaoyu);
		xiaoyu.setTag(0);
		xiaoyu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if ("0".equals(xiaoyu.getTag().toString())) {

					xiaoyu.setImageResource(R.drawable.select_on);
					xiaoyu.setTag("1");
				} else {

					xiaoyu.setImageResource(R.drawable.select);
					xiaoyu.setTag("0");
				}
			}
		});
		et_zhuceyonghun = (EditText) findViewById(R.id.et_zhuceyonghun);
		et_yanzhengma = (EditText) findViewById(R.id.et_yanzhengma);
		et_zhucemima = (EditText) findViewById(R.id.et_zhucemima);
		et_zaicimima = (EditText) findViewById(R.id.et_zaicimima);

	}

	private void initTitleBar() {
		TextView tv_title_back = (TextView) findViewById(R.id.tv_title_back);
		TextView tv_title_title = (TextView) findViewById(R.id.tv_title_title);

		btn_next = (Button) findViewById(R.id.btn_next);
		btn_next.setOnClickListener(this);
		tv_title_back.setVisibility(View.VISIBLE);
		tv_title_back.setOnClickListener(this);
		tv_title_title.setText(getString(R.string.register));

	}

	private void yanzheng() {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("mobileaccount", "p_" + et_zhuceyonghun.getText().toString());
		map.put("messageType", "0");
		TransferParameter parameter = new TransferParameter();
		parameter.setUsername("");
		parameter.setPassword("");
		parameter.setData(map);
		new HttpServer().post(HttpCons.MobileMessageVerify, parameter, Handler);

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tv_title_back:
			finish();
			break;
		case R.id.bt_yanzhengma:
			if (TextUtils.isEmpty(et_zhuceyonghun.getText().toString())) {
				Toast.makeText(getApplicationContext(), MyConst.DENG_LU, 0)
						.show();

			} else if (et_zhuceyonghun.getText().toString().length() != 11) {
				Toast.makeText(getApplicationContext(), MyConst.YONGHUMING, 0)
						.show();
			} else {
				yanzheng();
			}
			break;
		case R.id.btn_next:

			if (TextUtils.isEmpty(et_zhuceyonghun.getText().toString())) {
				Toast.makeText(getApplicationContext(), MyConst.DENG_LU, 0)
						.show();
				return;
			} else if (et_zhuceyonghun.getText().toString().length() != 11) {
				Toast.makeText(getApplicationContext(), MyConst.YONGHUMING, 0)
						.show();
				return;
			} else if (TextUtils.isEmpty(et_zhucemima.getText().toString())) {
				Toast.makeText(getApplicationContext(), MyConst.MIMA, 0).show();
				return;
			} else if (TextUtils.isEmpty(et_zaicimima.getText().toString())) {
				Toast.makeText(getApplicationContext(), MyConst.MIMA, 0).show();
				return;
			} else if (TextUtils.isEmpty(et_yanzhengma.getText().toString())) {

				Toast.makeText(getApplicationContext(), "验证码不能为空", 0).show();
				return;
			} else if (!et_zhucemima.getText().toString()
					.equals(et_zaicimima.getText().toString())) {

				Toast.makeText(getApplicationContext(), "兩次輸入不一致", 0).show();
				return;
			} else if (!("1".equals(xiaoyu.getTag().toString()))) {
				Toast.makeText(getApplicationContext(), "请同意服务条款", 0).show();
				return;
			} else if (et_zaicimima.getText().toString()
					.matches("(?!^\\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]{6,10}")) {
				Toast.makeText(RegisterActivity.this, "密码请输入6-10位字母数字组合",
						Toast.LENGTH_SHORT).show();
				return;
			} else {
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("confimPassword", et_zaicimima.getText().toString());
				map.put("aliasAccount", "p_"
						+ et_zhuceyonghun.getText().toString());
				map.put("mobileVerifyCode", et_yanzhengma.getText().toString());
				TransferParameter parameter = new TransferParameter();
				parameter.setUsername(et_zhuceyonghun.getText().toString());
				parameter.setPassword(et_zaicimima.getText().toString());
				parameter.setData(map);
				Log.e("RRR", map + "");
				new HttpServer().post(HttpCons.Insert_Patient, parameter,
						ReHandler);
				btn_next = (Button) findViewById(R.id.bt_yanzhengma);
				btn_next.setOnClickListener(this);
			}
			break;
		default:
			break;
		}

	}
}
